// pages/my/cash/cash.js

const App = getApp()
const api = require('../../../utils/api.js');

Page({
  data: {
    cashTotal:0,
    cash: {
      items: [
        /**
         * 
            {
              name: '淘淘领返款',
              time: '2017-10-31 22:45:10',
              cash: '+20'
            },
            {
              name: '转帐',
              time: '2017-10-31 22:45:10',
              cash: '-30'
            },
            {
              name: '充值',
              time: '2017-10-31 22:45:10',
              cash: '+60'
            }        
         */

      ],
      params: {
        page: 0,
        limit: 10,
      },
      paginate: {}
    },

    prompt: {
      hidden: 1,
      icon: '/image/empty.png',
      title: '您还没有零钱！',
      text: '赶快去充值吧',
    },
  },




  onLoad:function(options) {


    this.setData({
      cashTotal: options.cashTotal
    })

    this.onPullDownRefresh()
  },



  //零钱流水
  cashList: function (e) {

    let paramsData = {
      member_id: App.globalData.memberInfo.member_id,
      member_type_key: 'consumer',
    }

    let pageData = {
      page_no: this.data.cash.params.page + 1,
      page_size:this.data.cash.params.limit,
    }

      //零钱列表接口
    const data = {
      service: 'finance.memberCashLogPageFind',
      params: JSON.stringify(paramsData),
      page: JSON.stringify(pageData),
      url_type: 'finance',
      app_token: App.globalData.app_token,        
      security_code: App.globalData.security_code  
    }

    var that = this

    const cash = this.data.cash
    const prompt = this.data.prompt
    const params = cash.params
    var total;

    api.reqData({
      data,
      success: (res) => {
        console.log("success 返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);

        cash.items = [...cash.items, ...resData.data.list]
        cash.params.page = resData.data.page.page_no
        cash.params.limit = resData.data.page.page_size

        cash.paginate.total = resData.data.page.total_count

        //判断hasNext
        if (cash.paginate.total > cash.params.page * cash.params.limit) {
          console.log("后面有数据 还没有查询完")
          cash.paginate.hasNext = true
        }
        prompt.hidden = cash.items.length

        //console.log("total>>>>>"+total)
        //console.log("cash.params>>>>>" + cash.params)

        this.setData({
          cash: cash,
          //'prompt.hidden': cash.items.length,
          prompt: prompt,
        })

      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      }
    })
  },




  //获得零钱余额
  usableCashBalanceFind: function (e) {



      let paramsData = {
        member_id: App.globalData.memberInfo.member_id,
        member_type_key: 'consumer',
      }
       //查询零钱接口
      const data = {
        service: 'finance.usableCashBalanceFind',
        params: JSON.stringify(paramsData),
        url_type: 'finance',
        app_token: App.globalData.app_token,
        security_code: App.globalData.security_code 
      }

      var that = this
      api.reqData({
        data,
        success: (res) => {
          var resData = res.data;
          if (resData.rsp_code == 'succ') {
            that.setData({
              cashTotal: resData.data.usable_cash_balance,
            });
            that.update();
          }
        },
        fail: (res) => {
          console.log(res.data);
        }
      })
    
  },


  /***
   * 
    getList() {
      const cash = this.data.cash
      const params = cash.params
  
      const data = {
        service:'finance.memberCashLogPageFind',
        params:'{"member_id":"10494374","member_type_key":"consumer"}',
        page:'{"page_no":"1","page_size":"10"}',
        //url_type: 'finance',
      }
  
      // // App.HttpService.getCashList(params)
      this.cash.queryAsync(data)
        .then(res => {
          const data = res.data
          console.log(data)
  
          if (data.rsp_code == 'succ') {
            cash.items = [...cash.items, ...data.data.list]
            cash.paginate = data.data.paginate
            cash.params.page = data.data.page.page_no
            cash.params.limit = data.data.page.page_size
            this.setData({
              cash: cash,
              'prompt.hidden': cash.items.length,
            })
          }
        })
    },
   */


  toRecharge(e) {
    console.log(e)
    
    //App.WxService.navigateTo('/pages/my/cash/recharge')

    wx.navigateTo({
      url: '/pages/my/cash/recharge?cashTotal=' + this.data.cashTotal,
    });

  },


  initData() {
    this.setData({
      cash: {
        items: [],
        params: {
          page: 0,
          limit: 10,
        },
        paginate: {}
      }
    })
  },





  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    this.initData()
    this.cashList()
      // complete
    wx.showLoading({
      title: '加载中',
    })

    setTimeout(function () {
      wx.hideLoading()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.info('onReachBottom')
    if (!this.data.cash.paginate.hasNext) return
    this.cashList()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    //获得零钱余额
    this.usableCashBalanceFind()

    this.onPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }








})


